<?php

declare(strict_types=1);


namespace app\api\controller;

use app\common\tool\Result;
use app\common\model\MakeOrder as Order;
use app\common\controller\Config;
use think\Response;

class MakeOrder extends Base
{
    /**
     * 预约提交
     */
    public function submit(): Response
    {
        try {
            $params = $this->request->post();
            $this->validate($params, 'MakeOrder');
            return Result::fastReturn(Order::submitOrder($params, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 预约订单详情
     */
    public function getOrderInfo($id)
    {
        try {
            $info = Order::where(['uid' => $this->user->id, 'id' => $id])->find();
            if (empty($info)) return Result::fail('无效订单');
            $info->statusCode = $info->getData('status');
            $info->make_times = date('Y-m-d H:i', $info->getData('make_time'));
            $info->friendlyText = getProductOrder($info->statusCode);
            $info->typeCode = $info->getData('type');
            $info->tel = Config::lists()['contact_mobile'];
            $map = [
                'address' => Config::lists()['address'],
                'lat' => explode(',', Config::lists()['maplatlng'])[0],
                'lng' => explode(',', Config::lists()['maplatlng'])[1],
            ];
            $info->map = $map;
            $info->car_typename = $info->car_type>0?Order::getCarType($info->car_type):'';
            return Result::success('获取成功！', $info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 预约订单列表
     */
    public function getList(): Response
    {
        try {
            $param = $this->request->get();
            $map[] = ['uid', '=', $this->user->id];
            if (isset($param['status']) && $param['status']) {
                $map[] = ['status', '=', $param['status']];
            }
            $list = Order::getPageList($map, 'id,sn,status,title,sub_title,money,icon,make_time', $this->getPage(), 20, false);
            foreach ($list['data'] as &$v) {
                $v->make_times = date('Y-m-d H:i', $v->getData('make_time'));
                $v->statusCode = $v->getData('status');
                $v->friendlyText = getProductOrder($v->statusCode);
            }
            return Result::success('获取成功！', $list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 订单状态操作
     */
    public function edit()
    {
        try {
            $param = $this->request->post();
            return Result::fastReturn(Order::editOrder($param, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
